package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * 7837
 * http://bbs.yinsha.com/ 碧海银沙论坛
 * @author KK
 */
public class YinshaBbsAnalyse implements AnalyseNews{

	public YinshaBbsAnalyse() {
//		CookieManager.getInstance().storeCookies("yinsha.com", "PHPSESSID=1tfg7rv148m0usjjpjedusjnk0; _pk_id.6.bd67=2e8b8565e965b160.1378456276.1.1378456276.1378456276.; _pk_ses.6.bd67=*; PHPSESSID=dc34a75a36b5d087b77f9ccf076b2792; ppidysidb=a2traXNzeQ%3D%3D.4bbdcc0e821637155ac4217bdab70d2e.430a27748f3853d3745bda9d32419fef.ZGMzNGE3NWEzNmI1ZDA4N2I3N2Y5Y2NmMDc2YjI3OTI%3D.0b44a06aee75ba0199a8ffd37c5cc533");
	}

	
	public boolean isDetailPage(String url){
		boolean bRet = false;
		String[] regex = {
				"http://bbs.yinsha.com/\\?bid=[0-9]*&m=t",
				"http://bbs.yinsha.com/\\?bid=[0-9]*&gid=[0-9]*&m=t",
				"http://bbs.yinsha.com/\\?bid=[0-9]*&m=t&page=1",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String author = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);
		title =doc.select("title").text();
		if (title!=null){
			title = StringUtils.substringBefore(title, " - ");
		}

		Element firstTable = doc.select("div#midContent > table").first();
		if (firstTable!=null){
			date = DateUtils.matchDate(firstTable.select("dl.chd ").text());
			content = HtmlCleaner.getContentHtml(url, firstTable.select("div.nr"));

			author = firstTable.select("div.author a.man").text();
			String commTxt = firstTable.select("span:containsOwn(查看:)").text();
			if (commTxt!=null){
				clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "查看:"));
				commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复:"));
			}
		}


		if(StringUtils.isBlank(title)){
		}
		bbs.setAuthor(author);
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

    

}
